<template>
  <div class="login">
    <!-- 背景装饰元素 -->
    <div class="bg-decoration">
      <div class="floating-hearts">
        <div class="heart" v-for="i in 6" :key="i"></div>
      </div>
      <div class="gradient-orbs">
        <div class="orb orb-1"></div>
        <div class="orb orb-2"></div>
        <div class="orb orb-3"></div>
      </div>
    </div>
    
    <div class="login-container">
      <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
        <div class="form-header">
          <div class="logo-area">
            <div class="logo-icon">
              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
                <path d="M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z"/>
              </svg>
            </div>
          </div>
          <h1 class="form-title">💕 相亲交友平台</h1>
          <p class="form-subtitle">找到你的另一半，开启美好人生</p>
          <div class="welcome-text">
            <h3>管理员登录</h3>
            <p>请输入您的登录凭据</p>
          </div>
        </div>
        
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            size="large"
            auto-complete="off"
            placeholder="用户名"
            class="custom-input"
          >
            <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
          </el-input>
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            size="large"
            auto-complete="off"
            placeholder="密码"
            class="custom-input"
            @keyup.enter="handleLogin"
          >
            <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
          </el-input>
        </el-form-item>
        
        <el-form-item prop="code" v-if="captchaEnabled">
          <el-input
            v-model="loginForm.code"
            size="large"
            auto-complete="off"
            placeholder="验证码"
            style="width: 63%"
            class="custom-input"
            @keyup.enter="handleLogin"
          >
            <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img"/>
          </div>
        </el-form-item>
        
        <div class="form-options">
          <el-checkbox v-model="loginForm.rememberMe" class="remember-me">记住密码</el-checkbox>
          <a href="#" class="forgot-password">忘记密码？</a>
        </div>
        
        <el-form-item style="width:100%; margin-bottom: 0;">
          <el-button
            :loading="loading"
            size="large"
            type="primary"
            class="login-button"
            @click.prevent="handleLogin"
          >
            <span v-if="!loading">登录</span>
            <span v-else>登录中...</span>
          </el-button>
        </el-form-item>
        
        <div class="form-footer" v-if="register">
          <span>还没有账号？</span>
          <router-link class="register-link" :to="'/register'">立即注册</router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { getCodeImg } from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from "@/utils/jsencrypt"
import useUserStore from '@/store/modules/user'

const title = import.meta.env.VITE_APP_TITLE
const userStore = useUserStore()
const route = useRoute()
const router = useRouter()
const { proxy } = getCurrentInstance()

const loginForm = ref({
  username: "admin",
  password: "admin123",
  rememberMe: false,
  code: "",
  uuid: ""
})

const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }]
}

const codeUrl = ref("")
const loading = ref(false)
// 验证码开关
const captchaEnabled = ref(true)
// 注册开关
const register = ref(false)
const redirect = ref(undefined)

watch(route, (newRoute) => {
    redirect.value = newRoute.query && newRoute.query.redirect
}, { immediate: true })

function handleLogin() {
  proxy.$refs.loginRef.validate(valid => {
    if (valid) {
      loading.value = true
      // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
      if (loginForm.value.rememberMe) {
        Cookies.set("username", loginForm.value.username, { expires: 30 })
        Cookies.set("password", encrypt(loginForm.value.password), { expires: 30 })
        Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 })
      } else {
        // 否则移除
        Cookies.remove("username")
        Cookies.remove("password")
        Cookies.remove("rememberMe")
      }
      // 调用action的登录方法
      userStore.login(loginForm.value).then(() => {
        const query = route.query
        const otherQueryParams = Object.keys(query).reduce((acc, cur) => {
          if (cur !== "redirect") {
            acc[cur] = query[cur]
          }
          return acc
        }, {})
        router.push({ path: redirect.value || "/", query: otherQueryParams })
      }).catch(() => {
        loading.value = false
        // 重新获取验证码
        if (captchaEnabled.value) {
          getCode()
        }
      })
    }
  })
}

function getCode() {
  getCodeImg().then(res => {
    captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.img
      loginForm.value.uuid = res.uuid
    }
  })
}

function getCookie() {
  const username = Cookies.get("username")
  const password = Cookies.get("password")
  const rememberMe = Cookies.get("rememberMe")
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password: password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  }
}

getCode()
getCookie()
</script>

<style lang='scss' scoped>
.login {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 50%, #f093fb 100%);
  position: relative;
  overflow: hidden;
}

// 背景装饰
.bg-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
}

.floating-hearts {
  position: absolute;
  width: 100%;
  height: 100%;
  
  .heart {
    position: absolute;
    width: 20px;
    height: 20px;
    background: rgba(255, 255, 255, 0.1);
    transform: rotate(45deg);
    animation: float 6s ease-in-out infinite;
    
    &:before,
    &:after {
      content: '';
      position: absolute;
      width: 20px;
      height: 20px;
      background: rgba(255, 255, 255, 0.1);
      border-radius: 50%;
    }
    
    &:before {
      top: -10px;
      left: 0;
    }
    
    &:after {
      top: 0;
      left: -10px;
    }
    
    &:nth-child(1) { top: 20%; left: 10%; animation-delay: 0s; }
    &:nth-child(2) { top: 60%; left: 20%; animation-delay: 1s; }
    &:nth-child(3) { top: 40%; left: 80%; animation-delay: 2s; }
    &:nth-child(4) { top: 80%; left: 70%; animation-delay: 3s; }
    &:nth-child(5) { top: 10%; left: 60%; animation-delay: 4s; }
    &:nth-child(6) { top: 70%; left: 90%; animation-delay: 5s; }
  }
}

.gradient-orbs {
  .orb {
    position: absolute;
    border-radius: 50%;
    filter: blur(40px);
    animation: float 8s ease-in-out infinite;
  }
  
  .orb-1 {
    width: 300px;
    height: 300px;
    background: radial-gradient(circle, rgba(255, 105, 180, 0.3) 0%, transparent 70%);
    top: -150px;
    left: -150px;
    animation-delay: 0s;
  }
  
  .orb-2 {
    width: 200px;
    height: 200px;
    background: radial-gradient(circle, rgba(138, 43, 226, 0.3) 0%, transparent 70%);
    bottom: -100px;
    right: -100px;
    animation-delay: 2s;
  }
  
  .orb-3 {
    width: 250px;
    height: 250px;
    background: radial-gradient(circle, rgba(255, 20, 147, 0.2) 0%, transparent 70%);
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    animation-delay: 4s;
  }
}

@keyframes float {
  0%, 100% { transform: translateY(0px) rotate(0deg); }
  50% { transform: translateY(-20px) rotate(180deg); }
}

.login-container {
  width: 450px;
  background: rgba(255, 255, 255, 0.15);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 24px;
  box-shadow: 
    0 25px 45px rgba(0, 0, 0, 0.1),
    inset 0 1px 0 rgba(255, 255, 255, 0.2);
  padding: 50px 40px;
  position: relative;
  z-index: 2;
  animation: slideUp 0.8s ease-out;
}



.logo {
  display: flex;
  align-items: center;
}



.description {
  font-size: 14px;
  line-height: 1.5;
}




  
@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-form {
  width: 100%;
}

.form-header {
  text-align: center;
  margin-bottom: 40px;
  
  .logo-area {
    margin-bottom: 20px;
    
    .logo-icon {
      width: 80px;
      height: 80px;
      background: linear-gradient(135deg, #ff6b9d 0%, #c44569 100%);
      border-radius: 20px;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      color: white;
      margin: 0 auto 20px;
      box-shadow: 
        0 10px 30px rgba(255, 107, 157, 0.3),
        inset 0 1px 0 rgba(255, 255, 255, 0.2);
      transition: all 0.3s ease;
      animation: pulse 2s ease-in-out infinite;
      
      svg {
        width: 40px;
        height: 40px;
      }
      
      &:hover {
        transform: translateY(-5px) scale(1.05);
        box-shadow: 0 15px 40px rgba(255, 107, 157, 0.4);
      }
    }
  }
  
  .form-title {
    font-size: 32px;
    font-weight: 700;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    margin-bottom: 10px;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  }
  
  .form-subtitle {
    color: rgba(255, 255, 255, 0.8);
    font-size: 16px;
    margin-bottom: 30px;
    font-weight: 300;
  }
  
  .welcome-text {
    h3 {
      color: white;
      font-size: 20px;
      font-weight: 600;
      margin-bottom: 8px;
    }
    
    p {
      color: rgba(255, 255, 255, 0.7);
      font-size: 14px;
      margin: 0;
    }
  }
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.05); }
}
  
.custom-input {
  margin-bottom: 20px;
  
  :deep(.el-input__wrapper) {
    border-radius: 16px;
    border: 1px solid rgba(255, 255, 255, 0.3);
    box-shadow: none;
    transition: all 0.3s ease;
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(10px);
    height: 50px;
    
    &:hover {
      border-color: rgba(255, 255, 255, 0.5);
      background: rgba(255, 255, 255, 0.15);
      transform: translateY(-2px);
      box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
    }
    
    &.is-focus {
      border-color: rgba(255, 255, 255, 0.6);
      background: rgba(255, 255, 255, 0.2);
      box-shadow: 
        0 0 0 3px rgba(255, 255, 255, 0.1),
        0 8px 25px rgba(0, 0, 0, 0.15);
      transform: translateY(-2px);
    }
  }
  
  :deep(.el-input__inner) {
    height: 48px;
    font-size: 16px;
    color: white;
    padding: 0 20px;
    font-weight: 400;
    
    &::placeholder {
      color: rgba(255, 255, 255, 0.6);
    }
  }
  
  :deep(.el-input__prefix) {
    left: 15px;
  }
}
  
.input-icon {
  color: rgba(255, 255, 255, 0.7);
  font-size: 18px;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  
  .remember-me {
    :deep(.el-checkbox__label) {
      color: rgba(255, 255, 255, 0.8);
      font-size: 14px;
      font-weight: 400;
    }
    
    :deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
      background-color: #ff6b9d;
      border-color: #ff6b9d;
    }
    
    :deep(.el-checkbox__inner) {
      border-color: rgba(255, 255, 255, 0.4);
      background: rgba(255, 255, 255, 0.1);
    }
  }
  
  .forgot-password {
    color: rgba(255, 255, 255, 0.8);
    text-decoration: none;
    font-size: 14px;
    transition: all 0.3s ease;
    padding: 5px 10px;
    border-radius: 8px;
    
    &:hover {
      color: white;
      background: rgba(255, 255, 255, 0.1);
      transform: translateY(-1px);
    }
  }
}

.login-button {
  width: 100%;
  height: 54px;
  border-radius: 16px;
  background: linear-gradient(135deg, #ff6b9d 0%, #c44569 100%);
  border: none;
  font-size: 18px;
  font-weight: 600;
  color: white;
  margin-top: 10px;
  transition: all 0.3s ease;
  box-shadow: 
    0 8px 25px rgba(255, 107, 157, 0.3),
    inset 0 1px 0 rgba(255, 255, 255, 0.2);
  
  &:hover {
    background: linear-gradient(135deg, #ff5582 0%, #b73e56 100%);
    transform: translateY(-3px);
    box-shadow: 
      0 12px 35px rgba(255, 107, 157, 0.4),
      inset 0 1px 0 rgba(255, 255, 255, 0.2);
  }
  
  &:active {
    transform: translateY(-1px);
    box-shadow: 
      0 6px 20px rgba(255, 107, 157, 0.3),
      inset 0 1px 0 rgba(255, 255, 255, 0.2);
  }
}

.form-footer {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  color: rgba(255, 255, 255, 0.7);
  margin-top: 30px;
  gap: 8px;
  
  a {
    color: white;
    text-decoration: none;
    font-weight: 500;
    transition: all 0.3s ease;
    padding: 5px 10px;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.1);
    
    &:hover {
      background: rgba(255, 255, 255, 0.2);
      transform: translateY(-1px);
    }
  }
}

.login-code {
  width: 35%;
  height: 50px;
  float: right;
  display: flex;
  align-items: center;
  justify-content: center;
  
  .login-code-img {
    height: 48px;
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.3s ease;
    border: 1px solid rgba(255, 255, 255, 0.3);
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(10px);
    
    &:hover {
      border-color: rgba(255, 255, 255, 0.5);
      background: rgba(255, 255, 255, 0.15);
      transform: translateY(-2px);
      box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .login-container {
    width: 95%;
    max-width: 400px;
    padding: 30px 25px;
    margin: 20px;
  }
  
  .form-header {
    margin-bottom: 30px;
    
    .logo-area .logo-icon {
      width: 60px;
      height: 60px;
      
      svg {
        width: 30px;
        height: 30px;
      }
    }
    
    .form-title {
      font-size: 24px;
    }
    
    .form-subtitle {
      font-size: 14px;
    }
    
    .welcome-text h3 {
      font-size: 18px;
    }
  }
  
  .custom-input {
    margin-bottom: 15px;
    
    :deep(.el-input__wrapper) {
      height: 45px;
    }
    
    :deep(.el-input__inner) {
      height: 43px;
      font-size: 14px;
    }
  }
  
  .login-button {
    height: 48px;
    font-size: 16px;
  }
}
</style>
